package com.easygem.front.service.imp;

import com.easygem.common.model.system.Dept;
import com.easygem.common.model.system.User;
import com.easygem.common.response.ActiveUser;
import com.easygem.common.utils.DepUtils;
import com.easygem.common.vo.system.DepartmentVO;
import com.easygem.common.vo.system.UserVO;
import com.easygem.front.converter.BUserConverter;
import com.easygem.front.converter.DepartmentConverter;
import com.easygem.front.mapper.BUserMapper;
import com.easygem.front.mapper.DepartmentMapper;
import com.easygem.front.service.DepartmentService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 罗力
 * @version 1.0
 * @className DepartmentServiceImpl
 * @description TODO 某某类
 * @date 2024/12/26 20:23
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private BUserMapper userMapper;

    /**
     * 根据id获取部门以及下级部门信息
     * @return 部门集合
     */
    @Override
    public List<DepartmentVO> getDepartmentListByUser() {
        //获取当前登录用户对象
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        List<DepartmentVO> list = new ArrayList<DepartmentVO>();

        List<Dept> deptList=null;
        if (activeUser.getUser()!=null){
            //获取下级相关部门
            List<Long> ids= DepUtils.getDepartmentAndChildrenIds(departmentMapper.selectAll(),activeUser.getUser().getDeptId());

            //获取部门信息
            deptList=departmentMapper.getDepartmentByIds(ids);
        }else {
            deptList=departmentMapper.selectAll();
        }
        for (Dept dept :deptList) {
            list.add(DepartmentConverter.departmentToDepartmentVO(dept));
        }
        return list;
    }

    /**
     * 根据map集合获取用户列表
     * @return 用户列表
     */
    @Override
    public List<UserVO> getUserListByMap(User user) {
        //获取当前登录用户对象
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        List<User> userList=  null;
        if (activeUser.getUser()!=null) {
            /**
             * 等级为普通员工时只展示自己
             */
            if (activeUser.getUser().getType()==2) {
                user.setUserId(activeUser.getUser().getUserId());
            }
            user.setAccountName(activeUser.getUser().getAccountName());
            List<Long> ids= DepUtils.getDepartmentAndChildrenIds(departmentMapper.selectAll(),activeUser.getUser().getDeptId());
        }
        userList=userMapper.selectUserList(user);
        //移除非部门编号集合中的用户
//        userList.removeIf(user1 ->!ids.contains(user1.getDeptId()));

        return new BUserConverter().converterToUserVOList(userList);
    }
}
